<template>
	<div class="personal layout-pd">
		<el-row>
			<!-- 个人信息 -->
			<el-col :xs="24" :sm="16">
				<el-card shadow="hover" header="个人信息">
					<div class="personal-user">
						<div class="personal-user-left">
							<el-upload class="h100 personal-user-left-upload"
								accept="image/gif,image/jpeg,image/jpg,image/png,image/svg+xml"
								action="https://jsonplaceholder.typicode.com/posts/" multiple :limit="1">
								<img :src="state.personalForm.avatar" />
							</el-upload>
						</div>
						<div class="personal-user-right">
							<el-row>
								<el-col :span="24" class="personal-title mb18">{{ currentTime }}，{{
									state.personalForm.userName }}
								</el-col>
								<el-col :span="24">
									<el-row>
										<el-col :xs="24" :sm="8" class="personal-item mb6">
											<div class="personal-item-label">昵称：</div>
											<div class="personal-item-value">{{ state.personalForm.nickName }}</div>
										</el-col>
										<el-col :xs="24" :sm="16" class="personal-item mb6">
											<div class="personal-item-label">身份：</div>
											<div class="personal-item-value" v-for="(v, k) in state.personalForm.roles"
												:key="k">{{ v.roleName }}&nbsp;&nbsp;</div>
										</el-col>
									</el-row>
								</el-col>
								<el-col :span="24">
									<el-row>
										<el-col :xs="24" :sm="8" class="personal-item mb6">
											<div class="personal-item-label">登录IP：</div>
											<div class="personal-item-value">{{ state.personalForm.loginIp }}</div>
										</el-col>
										<el-col :xs="24" :sm="16" class="personal-item mb6">
											<div class="personal-item-label">登录时间：</div>
											<div class="personal-item-value">{{ state.personalForm.loginDate }}</div>
										</el-col>
									</el-row>
								</el-col>
							</el-row>
						</div>
					</div>
				</el-card>
			</el-col>

			<!-- 消息通知 -->
			<el-col :xs="24" :sm="8" class="pl15 personal-info">
				<el-card shadow="hover">
					<template #header>
						<span>活动通知</span>
						<span class="personal-info-more">更多</span>
					</template>
					<div class="personal-info-box">
						<ul class="personal-info-ul">
							<li v-for="(v, k) in state.newsInfoList" :key="k" class="personal-info-li">
								<a :href="v.link" target="_block" class="personal-info-li-title">{{
									v.title
								}}</a>
							</li>
						</ul>
					</div>
				</el-card>
			</el-col>

			<!-- 营销推荐 -->
			<el-col :span="24">
				<el-card shadow="hover" class="mt15" header="营销推荐">
					<el-row :gutter="15" class="personal-recommend-row">
						<el-col :sm="6" v-for="(v, k) in state.recommendList" :key="k" class="personal-recommend-col">
							<div class="personal-recommend" :style="{ 'background-color': v.bg }">
								<SvgIcon :name="v.icon" :size="70" :style="{ color: v.iconColor }" />
								<div class="personal-recommend-auto">
									<div>{{ v.title }}</div>
									<div class="personal-recommend-msg">{{ v.msg }}</div>
								</div>
							</div>
						</el-col>
					</el-row>
				</el-card>
			</el-col>

			<!-- 更新信息 -->
			<el-col :span="24">
				<el-card shadow="hover" class="mt15 personal-edit" header="更新信息">
					<div class="personal-edit-title">基本信息</div>
					<el-form :model="state.personalForm" size="default" label-width="40px" class="mt35 mb35"
						ref="updateForm">
						<el-row :gutter="35">
							<el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="4" class="mb20">
								<el-form-item label="昵称" prop="nickName">
									<el-input v-model="state.personalForm.nickName" placeholder="请输入昵称"
										clearable></el-input>
								</el-form-item>
							</el-col>
							<el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="4" class="mb20">
								<el-form-item label="邮箱" prop="email">
									<el-input v-model="state.personalForm.email" placeholder="请输入邮箱" clearable></el-input>
								</el-form-item>
							</el-col>
							<el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="4" class="mb20">
								<el-form-item label="手机" prop="phonenumber">
									<el-input v-model="state.personalForm.phonenumber" placeholder="请输入手机号"
										clearable></el-input>
								</el-form-item>
							</el-col>
							<el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="4" class="mb20">
								<el-form-item label="性别" prop="sex">
									<el-select v-model="state.personalForm.sex" placeholder="请选择性别" clearable class="w100">
										<el-option label="男" value="0"></el-option>
										<el-option label="女" value="1"></el-option>
										<el-option label="未知" value="2"></el-option>
									</el-select>
								</el-form-item>
							</el-col>
							<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
								<el-form-item>
									<el-button type="primary" @click="updatePersonalInfo">
										<el-icon>
											<ele-Position />
										</el-icon>
										更新个人信息
									</el-button>
								</el-form-item>
							</el-col>
						</el-row>
					</el-form>
					<div class="personal-edit-title mb15">账号安全</div>
					<div class="personal-edit-safe-box">
						<div class="personal-edit-safe-item">
							<div class="personal-edit-safe-item-left">
								<div class="personal-edit-safe-item-left-label">账户密码</div>
								<div class="personal-edit-safe-item-left-value">当前密码强度：强</div>
							</div>
							<div class="personal-edit-safe-item-right">
								<el-button text type="primary" @click="openUpdatePasswordDialog">立即修改</el-button>
							</div>
						</div>
					</div>
					<div class="personal-edit-safe-box">
						<div class="personal-edit-safe-item">
							<div class="personal-edit-safe-item-left">
								<div class="personal-edit-safe-item-left-label">密保手机</div>
								<div class="personal-edit-safe-item-left-value">
									已绑定手机：132****4108
								</div>
							</div>
							<div class="personal-edit-safe-item-right">
								<el-button text type="primary">立即修改</el-button>
							</div>
						</div>
					</div>
					<div class="personal-edit-safe-box">
						<div class="personal-edit-safe-item">
							<div class="personal-edit-safe-item-left">
								<div class="personal-edit-safe-item-left-label">密保问题</div>
								<div class="personal-edit-safe-item-left-value">
									已设置密保问题，账号安全大幅度提升
								</div>
							</div>
							<div class="personal-edit-safe-item-right">
								<el-button text type="primary">立即设置</el-button>
							</div>
						</div>
					</div>
					<div class="personal-edit-safe-box">
						<div class="personal-edit-safe-item">
							<div class="personal-edit-safe-item-left">
								<div class="personal-edit-safe-item-left-label">绑定QQ</div>
								<div class="personal-edit-safe-item-left-value">已绑定QQ：110****566</div>
							</div>
							<div class="personal-edit-safe-item-right">
								<el-button text type="primary">立即设置</el-button>
							</div>
						</div>
					</div>
				</el-card>
			</el-col>
		</el-row>
		<el-dialog :title="state.dialog.title" v-model="state.dialog.isShowDialog" width="569px" v-if="state.dialog.type === 'updatePassword'">
			<el-form ref="userDialogPwdRef" :model="state.ruleForm" :rules="state.updatePasswordRules" size="default" label-width="90px">
				<el-row :gutter="24">
					<el-col :span="24" class="mb20">
						<el-form-item label="旧密码" prop="oldPassword">
							<el-input v-model="state.ruleForm.oldPassword"  type="password"  placeholder="请输入旧密码" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :span="24" class="mb20">
						<el-form-item label="新密码" prop="newPassword">
							<el-input v-model="state.ruleForm.newPassword"  type="password"  placeholder="请输入新密码" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :span="24" class="mb20">
						<el-form-item label="确认旧密码" prop="confirmPassword">
							<el-input v-model="state.ruleForm.confirmPassword" type="password" placeholder="请确认你的新密码"
								maxlength="20"></el-input>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="closeDialog()" size="default">取 消</el-button>
					<el-button type="primary" @click="updatePassword" size="default">{{ state.dialog.submitTxt }}</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script setup lang="ts" name="personal">
import { reactive, computed } from 'vue';
import { formatAxis } from '/@/utils/formatTime';
import { Session } from '/@/utils/storage';
import { newsInfoList, recommendList } from './mock';
import { useUser } from '/@/api/system/user';
import { ElMessage } from 'element-plus';

// 定义变量内容
const state = reactive<PersonalState>({
	newsInfoList,
	recommendList,
	personalForm: {
		createBy: '',
		createTime: '',
		updateBy: '',
		updateTime: '',
		userId: '',
		deptId: 0,
		userName: 'admin',
		nickName: 'admin',
		password: '',
		userType: null,
		email: '123456789@qq.com',
		phonenumber: '132****4108',
		sex: '0',
		avatar: 'https://img2.baidu.com/it/u=1978192862,2048448374&fm=253&fmt=auto&app=138&f=JPEG?w=504&h=500',
		status: '0',
		delFlag: '0',
		loginIp: '192.168.1.1',
		loginDate: '',
		remark: '',
		dept: {
			createBy: '',
			createTime: '',
			updateBy: '',
			updateTime: '',
			parentName: '',
			parentId: 0,
			children: [],
			deptId: 0,
			deptName: '',
			orderNum: 0,
			leader: '',
			phone: '',
			email: '',
			status: '0',
			delFlag: '0',
			ancestors: '',
		},
		roles: [],
		roleIds: [],
		postIds: [],
		roleId: 0,
		admin: false,
	},
	ruleForm: {
		oldPassword: undefined,
		newPassword: undefined,
		confirmPassword: undefined,
	},
	updatePasswordRules:{
		oldPassword: [
			{ required: true, message: '请输入旧密码', trigger: 'blur' },
			{ min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' },
		],
		newPassword: [
			{ required: true, message: '请输入新密码', trigger: 'blur' },
			{ min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' },
		],
		confirmPassword: [
			{ required: true, message: '请确认密码', trigger: 'blur' },
			{ min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' },
		],
	},
	dialog: {
		isShowDialog: false,
		type: '',
		title: '',
		submitTxt: '确认',
	},
});


// 当前时间提示语
const currentTime = computed(() => {
	return formatAxis(new Date());
});
try {
	const userInfo = Session.getUserInfo();
	if (userInfo) {
		state.personalForm = userInfo.user;
	}
} catch (e) {
	Promise.reject(e);
}

const updatePersonalInfo = async () => {
	try {
		await useUser().updateUser({
			userId: state.personalForm.userId,
			userName: state.personalForm.userName,
			nickName: state.personalForm.nickName,
			phonenumber: state.personalForm.phonenumber,
			email: state.personalForm.email,
			state: state.personalForm.sex
		}).then((res) => {
			if (res.code === 200) {
				ElMessage.success(res.msg);
			} else {
				ElMessage.error(res.msg);
			}
		});
	} catch (e) {
		Promise.reject(e);
	}
};

const openUpdatePasswordDialog = () => {
	state.dialog.isShowDialog = true;
	state.dialog.type = 'updatePassword';
	state.dialog.title = '修改密码';
	state.dialog.submitTxt = '确认';
};

const updatePassword = async () => {
	try {
		const oldPassword = state.personalForm.password;
		const newPassword = state.personalForm.password;
		await useUser().updateUserPwd(oldPassword, newPassword).then((res) => {
			if (res.code === 200) {
				ElMessage.success(res.msg);
			} else {
				ElMessage.error(res.msg);
			}
		});
	} catch (e) {
		Promise.reject(e);
	}
};

/**
 * @description: 关闭弹窗
 * */
const closeDialog = () => {
	state.dialog.isShowDialog = false;
};

</script>

<style scoped lang="scss">
@import "../../theme/mixins/index.scss";

.personal {
	.personal-user {
		height: 130px;
		display: flex;
		align-items: center;

		.personal-user-left {
			width: 110px;
			height: 110px;
			border-radius: 3px;

			:deep(.el-upload) {
				height: 100%;
			}

			.personal-user-left-upload {
				img {
					width: 100%;
					height: 100%;
					border-radius: 3px;
				}

				&:hover {
					img {
						animation: logoAnimation 0.3s ease-in-out;
					}
				}
			}
		}

		.personal-user-right {
			flex: 1;
			padding: 0 15px;

			.personal-title {
				font-size: 18px;
				@include text-ellipsis(1);
			}

			.personal-item {
				display: flex;
				align-items: center;
				font-size: 13px;

				.personal-item-label {
					color: var(--el-text-color-secondary);
					@include text-ellipsis(1);
				}

				.personal-item-value {
					@include text-ellipsis(1);
				}
			}
		}
	}

	.personal-info {
		.personal-info-more {
			float: right;
			color: var(--el-text-color-secondary);
			font-size: 13px;

			&:hover {
				color: var(--el-color-primary);
				cursor: pointer;
			}
		}

		.personal-info-box {
			height: 130px;
			overflow: hidden;

			.personal-info-ul {
				list-style: none;

				.personal-info-li {
					font-size: 13px;
					padding-bottom: 10px;

					.personal-info-li-title {
						display: inline-block;
						@include text-ellipsis(1);
						color: var(--el-text-color-secondary);
						text-decoration: none;
					}

					& a:hover {
						color: var(--el-color-primary);
						cursor: pointer;
					}
				}
			}
		}
	}

	.personal-recommend-row {
		.personal-recommend-col {
			.personal-recommend {
				position: relative;
				height: 100px;
				border-radius: 3px;
				overflow: hidden;
				cursor: pointer;

				&:hover {
					i {
						right: 0px !important;
						bottom: 0px !important;
						transition: all ease 0.3s;
					}
				}

				i {
					position: absolute;
					right: -10px;
					bottom: -10px;
					font-size: 70px;
					transform: rotate(-30deg);
					transition: all ease 0.3s;
				}

				.personal-recommend-auto {
					padding: 15px;
					position: absolute;
					left: 0;
					top: 5%;
					color: var(--next-color-white);

					.personal-recommend-msg {
						font-size: 12px;
						margin-top: 10px;
					}
				}
			}
		}
	}

	.personal-edit {
		.personal-edit-title {
			position: relative;
			padding-left: 10px;
			color: var(--el-text-color-regular);

			&::after {
				content: "";
				width: 2px;
				height: 10px;
				position: absolute;
				left: 0;
				top: 50%;
				transform: translateY(-50%);
				background: var(--el-color-primary);
			}
		}

		.personal-edit-safe-box {
			border-bottom: 1px solid var(--el-border-color-light, #ebeef5);
			padding: 15px 0;

			.personal-edit-safe-item {
				width: 100%;
				display: flex;
				align-items: center;
				justify-content: space-between;

				.personal-edit-safe-item-left {
					flex: 1;
					overflow: hidden;

					.personal-edit-safe-item-left-label {
						color: var(--el-text-color-regular);
						margin-bottom: 5px;
					}

					.personal-edit-safe-item-left-value {
						color: var(--el-text-color-secondary);
						@include text-ellipsis(1);
						margin-right: 15px;
					}
				}
			}

			&:last-of-type {
				padding-bottom: 0;
				border-bottom: none;
			}
		}
	}
}
</style>
